<?php

/**
 * roditelj actions.
 *
 * @package    dnevnik
 * @subpackage roditelj
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class roditeljActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
      $razred = DbFinder::from('Razred')->join('Smer')->with('Smer')->findPk($request->getParameter('razred_id'));
      $smer = $razred->getSmer();
      if($smer->getTip() == SmerPeer::$tip['grupni'])
        isicsBreadcrumbs::getInstance()->addItem($razred.' '.$smer, 'razred/pregled?razred_id='.$request->getParameter('razred_id'));
      else  isicsBreadcrumbs::getInstance()->addItem($razred, 'razred/pregled?razred_id='.$request->getParameter('razred_id'));
      isicsBreadcrumbs::getInstance()->addItem('Roditelji');

      $this->getResponse()->addJavascript('ext/ext-base.js');
      $this->getResponse()->addJavascript('ext/ext-all.js');
      $this->getResponse()->addJavascript('ext/searchfield.js');
      $this->getResponse()->addJavascript('ext/roditelj_skola_grid.js');
      $this->getResponse()->addStylesheet('ext/ext-all.css');
  }

  public function executeGrid(sfWebRequest $request)
  {
      $grid = new Grid($request, 'roditelj');
      return $this->renderText($grid->render());
  }

  public function executeList(sfWebRequest $request)
  {
      $nbrows = 0;
      $arr = array();
      $roditeljs = DbFinder::from('Roditelj')->leftJoin('Roditelj.Id', 'Ucenik.RoditeljId')->leftJoin('Ucenik.RazredId', 'Razred.Id')->withColumn('Profil.Ime', 'ImeOtac')->withColumn('Profil.Prezime', 'PrezimeOtac')->withColumn('Razred.Razred', 'Razred')->withColumn('Razred.Slovo', 'Slovo')->where('SkolaId', 'in', $this->getUser()->getAttribute('skolas'))->find();
      foreach($roditeljs as $roditelj)
      {
        $rec['id'] = $roditelj->getId();
        $rec['naziv'] = $this->imeRod($roditelj).' - '.$roditelj->getColumn('Razred').'/'.$roditelj->getColumn('Slovo');
        $arr[] = $rec;
        $nbrows ++;
      }
      $jsonresult = json_encode($arr);
      return $this->renderText('({"total":"'.$nbrows.'","rows":'.$jsonresult.'})');
  }

  private function imeRod($roditelj)
  {
     if($roditelj->getColumn('ImeOtac') != '')
       return $roditelj->getColumn('PrezimeOtac').' '.$roditelj->getColumn('ImeOtac');
     else if($roditelj->getImeMajka() != '')
       return $roditelj->getPrezimeMajka().' '.$roditelj->getImeMajka();
     else if($roditelj->getImeStaratelj() != '')
       return $roditelj->getPrezimeStaratelj().' '.$roditelj->getImeStaratelj();
  }

  public function executeSkola(sfWebRequest $request)
  {
      $nbrows = 0;
      $arr = array();
      $credentials = new Credentials();
      $skolas = $credentials->getSkolaNazivs();
      foreach($skolas as $id => $naziv)
      {
        $rec['id'] = $id;
        $rec['naziv'] = $naziv;
        $arr[] = $rec;
        $nbrows ++;
      }
      $jsonresult = json_encode($arr);
      return $this->renderText('({"total":"'.$nbrows.'","rows":'.$jsonresult.'})');
  }
}
